package main.model;

import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import controller.editor.EditorController;

import main.model.persistence.DiagramXmlWriter;
import view.editor.CanvasView;
import view.editor.Layout;

public class Project {
	
	private HashMap<String,Diagram> diagramCollection = new HashMap<String, Diagram>();
	private HashMap<Diagram,LayoutComponents> layouts;
	private HashMap<Diagram,Layout> canvasLayouts;
	private List<String> componentsNames;
	private String name;
	private DiagramXmlWriter writer;

	public Project(String name){
		this.componentsNames = new ArrayList<String>();
		this.setDiagrams(new HashMap<String, Diagram>());
		this.layouts = new HashMap<Diagram, LayoutComponents>();
		this.canvasLayouts = new HashMap<Diagram, Layout>();
		this.name = name;
		this.writer = new DiagramXmlWriter();
	}
	
	public void addDiagramToCollection(Diagram d){
		this.diagramCollection.put(d.getName(), d);
	}
	
	public void addDiagram(Diagram d, Layout l){
		this.getDiagramCollection().put(d.getName(), d);
		this.layouts.put(d, new LayoutComponents());
		this.canvasLayouts.put(d, l);
	}
	
	public void addDiagram(Diagram d, LayoutComponents l){
		this.getDiagramCollection().put(d.getName(), d);
		this.layouts.put(d, l);
		Layout lay = new Layout(CanvasView.getInstance());
		this.canvasLayouts.put(d, lay);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Diagram getFirstDiagram() {
		if (!getDiagramCollection().isEmpty()){
			return getDiagramCollection().get("Principal");
		}
		return null;
	}

	public HashMap<String,Diagram> getDiagramCollection() {
		return diagramCollection;
	}

	public void setDiagrams(HashMap<String, Diagram> diagrams) {
		this.diagramCollection = diagrams;
	}
	
	public List<Diagram> getDiagrams(){
		Set<String> keySet = this.diagramCollection.keySet();
		ArrayList<Diagram> list = new ArrayList<Diagram>();
		for (String s : keySet) {
			Diagram d = this.diagramCollection.get(s);
			list.add(d);
		}
		return list;
	}

	public void saveAll() {
		Set<String> keySet = this.diagramCollection.keySet();
		for (String s : keySet) {
			Diagram d = this.diagramCollection.get(s);
			saveDiagram(d);
		}
	}
	
	public void saveDiagram(Diagram d){
		String path = this.name + "/" + d.getName() + "-comp.xml";
		String pathPositions = this.name + "/" + d.getName() + "-rep.xml";
		this.writer.writeTo(path, pathPositions, d, this.layouts, true);
	}

	public Diagram getDiagramByName(String diagramName) {
		return this.diagramCollection.get(diagramName);
	}
	
	public Point getPointForComponent(Diagram diagram, main.model.Component component){
		return this.layouts.get(diagram).getPositionComponent(component);
	}

	public void setNewPointToComponent(Diagram currentDiagram,
			main.model.Component component, Point p) {
		this.layouts.get(currentDiagram).setNewPointToComponent(component, p);
	}

	public void bindComponentToPoint(Diagram currentDiagram,
			main.model.Component component, Point p) {
		this.layouts.get(currentDiagram).getLayout().put(component, p);
	}

	public Layout getLayoutByDiagram(Diagram currentDiagram) {
		return this.canvasLayouts.get(currentDiagram);
	}

	public Entity getEntityInProjectByName(String selectedEntity) {
		Iterator<Diagram> diagramIt = this.getDiagrams().iterator();
		Boolean found = false;
		Entity e = null;
		while (diagramIt.hasNext() && !found){
			Diagram diagram = diagramIt.next();
			e = diagram.getEntityByName(selectedEntity);
			if (e != null){
				found = true;
			}
		}
		return e;
	}

	public List<String> getComponentsNames() {
		return componentsNames;
	}

	public void addComponentName(String name){
		this.componentsNames.add(name);
	}
	
	public void removeComponentName(String name){
		this.componentsNames.remove(name);
	}

	public void removePointForComponent(Diagram currentDiagram, main.model.Component c) {
		LayoutComponents layoutComponents = this.layouts.get(currentDiagram);
		layoutComponents.getLayout().remove(c);
	}

}
